package com.wanwu.channel.service.strategy;

import com.wanwu.channel.common.enums.ExceptionEnum;
import com.wanwu.channel.common.enums.PayType;
import com.wanwu.channel.common.enums.TradeStatusEnum;
import com.wanwu.channel.common.exception.SystemException;
import com.wanwu.channel.common.util.TrueFalse;

import com.wanwu.channel.converter.PayStatusConvert;
import com.wanwu.channel.dao.entity.ChannelRefundEntity;
import com.wanwu.channel.dto.vo.*;
import com.wanwu.channel.remote.alipay.config.ZfbGlobalConfiguration;
import com.wanwu.channel.remote.alipay.util.AlipaySubmit;
import com.wanwu.channel.service.strategy.domain.*;
import com.wanwu.channel.service.strategy.handle.AlipayHandle;
import com.wanwu.channel.service.strategy.parse.AlipayParse;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
//@EnableConfigurationProperties(ZfbGlobalConfiguration.class)
public class AlipayAppStrategy extends AbstractStragy {

    private static Logger logger = LoggerFactory.getLogger(AlipayAppStrategy.class);

    @Autowired
    private ZfbGlobalConfiguration zfbGlobalConfiguration;

    @Override
    public PrepayStrategyResponse generatePayParams(PayType payType, PrepayStrategyReqVo req) {
        PrepayStrategyResponse response = new PrepayStrategyResponse();
        String prepayOrderString = makeGlobalParam(req, zfbGlobalConfiguration);
        response.setTradeStatus(TradeStatusEnum.SUCCESS);
        response.setPrepayOrderString(prepayOrderString);
        //response.setPid(zfbGlobalConfiguration.getPid());
        //response.setAppId(zfbGlobalConfiguration.getSellerId());
        return response;
    }



    @Override
    public PayQueryStrategyRespVo queryPayHttp(PayType payType, PayQueryStrategyReqVo request) {
        PayQueryStrategyRespVo response = new PayQueryStrategyRespVo();
        Map<String, String> strMap = makeGlobalQueryParam(request, zfbGlobalConfiguration);
        String respStr =  AlipaySubmit.buildRequest(strMap, zfbGlobalConfiguration.getOpenApiDomain(), zfbGlobalConfiguration.getSignType(),
                zfbGlobalConfiguration.getPrivateKey(), zfbGlobalConfiguration.getInputCharset());
        Map<String, String> resultMap =  AlipayParse.doAlipayQueryReturnParse(respStr, zfbGlobalConfiguration.getPid(), zfbGlobalConfiguration.getAlipayPublicKey(),
                zfbGlobalConfiguration.getInputCharset());
        response.setThirdOrderNo(resultMap.get("trade_no"));
        response.setBizOrderNo(resultMap.get("out_trade_no"));
        if (TrueFalse.T.equalsIgnoreCase(resultMap.get("is_success"))) {
            response.setChannelPayStatus(PayStatusConvert.alipayConvert(resultMap.get("trade_status")));
            //response.setFinishTime(resultMap.get("gmt_payment"));
            response.setBuyerId(resultMap.get("buyer_id"));
            response.setBuyerEmail(resultMap.get("buyer_email"));
        }else{
            throw new SystemException(ExceptionEnum.E20003, resultMap.get("error_code"), resultMap.get("error_msg"));
        }
        return response;
    }



}
